package com.bdth.controller;

import com.bdth.model.AlarmConfig;
import com.bdth.service.IAlarmConfigService;
import com.bdth.utils.HtmlUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/alarmConfig")
public class AlarmConfigController extends BaseController {

	@Autowired
	private IAlarmConfigService alarmConfigService;


	protected static final Logger logger = LoggerFactory.getLogger(AlarmConfigController.class);

	/**
	 * 首页
	 *
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(HttpServletRequest request, String msg)
			throws Exception {
		Map<String, Object> context = getRootMap();
		Map<String, Object> map = new HashMap<String, Object>();
		List<AlarmConfig> dataList = alarmConfigService.queryByMapList(map);
		//设置页面数据
		context.put("alarmConfigs", dataList);
		boolean cbCheckAllAlarm = true;
		boolean cbCheckAllSound = true;
		boolean cbCheckAllPopup = true;
		for (AlarmConfig alarmConfig : dataList) {
			cbCheckAllAlarm = alarmConfig.getEnabled() && cbCheckAllAlarm;
			cbCheckAllSound = alarmConfig.getSoundEnabled() && cbCheckAllSound;
			cbCheckAllPopup = alarmConfig.getPopupEnabled() && cbCheckAllPopup;
		}
		context.put("message", msg);
		context.put("cbCheckAllAlarm", cbCheckAllAlarm);
		context.put("cbCheckAllSound", cbCheckAllSound);
		context.put("cbCheckAllPopup", cbCheckAllPopup);
		return forword("/alarmConfig/alarmConfigList", context);
	}

	@RequestMapping("/listAlarmConfig")
	public void listAlarmConfig(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("distinct", "distinct");
		List<AlarmConfig> dataList = alarmConfigService.queryByMapList(map);
		HtmlUtil.writerJson(response, dataList);
	}

	@RequestMapping("/config")
	public ModelAndView config(HttpServletRequest request, HttpServletResponse response, Integer[] alarmEnable, Integer[] alarmSoundEnable, Integer[] alarmPopupEnable) throws Exception {
		Map<String, Object> context = new HashMap<String, Object>();
		Map<String, Object> map = new HashMap<String, Object>();
		List<AlarmConfig> alarmConfigList = alarmConfigService.queryByMapList(map);
		Map<Integer, Integer> alarmEnableMap = new HashMap<Integer, Integer>();
		Map<Integer, Integer> soundEnableMap = new HashMap<Integer, Integer>();
		Map<Integer, Integer> popupEnableMap = new HashMap<Integer, Integer>();
		if (alarmEnable != null) {
			for (int typeId : alarmEnable) {
				alarmEnableMap.put(typeId, typeId);
			}
		}
		if (alarmSoundEnable != null) {
			for (int typeId : alarmSoundEnable) {
				soundEnableMap.put(typeId, typeId);
			}
		}
		if (alarmPopupEnable != null) {
			for (int typeId : alarmPopupEnable) {
				popupEnableMap.put(typeId, typeId);
			}
		}
		try {
			//批量更新
			List<AlarmConfig> lists = new ArrayList<AlarmConfig>();
			for (AlarmConfig ac : alarmConfigList) {

				int typeId = ac.getId();
				ac.setEnabled(alarmEnableMap.containsKey(typeId));
				ac.setSoundEnabled(soundEnableMap.containsKey(typeId));
				ac.setPopupEnabled(popupEnableMap.containsKey(typeId));
				lists.add(ac);
			}
			alarmConfigService.updateBatch(lists);
		} catch (Exception ex) {
			logger.error(ex.getMessage(), ex);
			return list(request, "保存失败");
		}
		return list(request, "保存成功");
	}

}
